﻿using System;
using System.Collections;
using System.Threading;
namespace PQ.Util
{
    public class Configuration
    {
        public int dev_num = 0;

        public byte ch_sel = 3;

        public double range = 10.0;
        //额定电压
        public static String RatingU = "额定电压(V)";

        //额定频率
        public static String RatingF = "额定频率(Hz)";

        //有效值最小时间间隔
        public static String RMSInterval = "RMS平均时间(ms)";

        //谐波最小时间间隔
        public static String HarmonicsInterval = "谐波平均时间(ms)";

        //自定义最小时间间隔
        public static String CustomInterval = "自定义平均时间(s)";

        //开始时间
        public static String StartTime = "开始时间";
        //结束时间
        public static String StopTime = "结束时间";

        
        public static String SampleFreq = "采样频率";

        public static String TriggerMode = "触发方式";

        public static String Range = "量程";

        //额定电压
        public  double ratingU ;

        //额定频率
        public  double ratingF ;

        //有效值最小时间间隔
        public  double rmsInterval ;

        //谐波最小时间间隔
        public  double harmonicsInterval ;

        //自定义最小时间间隔
        public  double customInterval ;

        //开始时间
        public  DateTime startTime ;
        //结束时间
        public DateTime stopTime;

        public byte triggerMode;
        public double sampleFreq;
        public string sampleFreqStr;

        public String format = "yyyy 年 MM 月 dd   hh:mm:ss";

        Hashtable cfg = new Hashtable();
        ArrayList bi_name = new ArrayList();
        ArrayList bi_value = new ArrayList();

        public ArrayList chArr = new ArrayList();

        

        public int indexUA=0, indexUB=1, indexUC=2, indexUN=3, indexUZ=4, indexIA=5, indexIB=6, indexIC=7, indexIN=8;
        public double Fua = 1.0, Fub = 1.0, Fuc = 1.0, Fun = 1.0, Fuz = 1.0, Fia = 1.0, Fib = 1.0, Fic = 1.0, Fin = 1.0;

        //用于两个线程之间交换数据的list
        public ArrayList dataExchange = new ArrayList();

        public void setHardwareConfig(String ch, String measuredName, double factor)
        {
            if(string.Equals(measuredName , "UA"))
            {
                indexUA = chArr.IndexOf(ch);
                Fua = factor;
            }
            if(string.Equals(measuredName , "UB"))
            {
                indexUB = chArr.IndexOf(ch);
                Fub = factor;
            }
            if(string.Equals(measuredName , "UC"))
            {
                indexUC = chArr.IndexOf(ch);
                Fuc = factor;
            }
            if(string.Equals(measuredName , "UN"))
            {
                indexUN = chArr.IndexOf(ch);
                Fun = factor;
            }
            if(string.Equals(measuredName , "UZ"))
            {
                indexUZ = chArr.IndexOf(ch);
                Fuz = factor;
            }
            if(string.Equals(measuredName , "IA"))
            {
                indexIA = chArr.IndexOf(ch);
                Fia = factor;
            }
            if(string.Equals(measuredName , "IB"))
            {
                indexIB = chArr.IndexOf(ch);
                Fib = factor;
            }
            if(string.Equals(measuredName , "IC"))
            {
                indexIC = chArr.IndexOf(ch);
                Fic = factor;
            }
            if(string.Equals(measuredName , "IN"))
            {
                indexIN = chArr.IndexOf(ch);
                Fin = factor;
            }
            
        }
        public Configuration()
        {
            chArr.Add("CH1");
            chArr.Add("CH2");
            chArr.Add("CH3");
            chArr.Add("CH4");
            chArr.Add("CH5");
            chArr.Add("CH6");
            chArr.Add("CH7");
            chArr.Add("CH8");
            chArr.Add("CH9");
            chArr.Add("CH10");
            chArr.Add("CH11");
            chArr.Add("CH12");

          
        }

        public void addBasicInfo(String name, String value)
        {
            bi_name.Add(name);
            bi_value.Add(value);
        }
        public int getBasicInfoSize()
        {
            return bi_name.Count;
        }
        public String getBasicInfoName(int i)
        {
            return bi_name[i].ToString();
        }
        public String getBasicInfoValue(int i)
        {
            return bi_value[i].ToString();
        }
        public String this[String index]
        {
            get
            {
                return (String)cfg[index];
            }
            set
            {
                if (cfg.Contains(index))
                {
                    cfg[index] = value;
                    
                    
                }
                else
                {
                    cfg.Add(index, value);
                }
                if (string.Equals(index, RatingU))
                {
                    ratingU = Convert.ToDouble(value);
                }
                if (string.Equals(index, RatingF))
                {
                    ratingF = Convert.ToDouble(value);
                }
                if (string.Equals(index, RMSInterval))
                {
                    rmsInterval = Convert.ToDouble(value);
                }
                if (string.Equals(index, HarmonicsInterval))
                {
                    harmonicsInterval = Convert.ToDouble(value);
                }
                if (string.Equals(index, CustomInterval))
                {
                    customInterval = Convert.ToDouble(value);
                }
                if (string.Equals(index, StartTime))
                {
                    startTime = DateTime.ParseExact(value, format, null);
                }
                if (string.Equals(index, StopTime))
                {
                    stopTime = DateTime.ParseExact(value, format, null);
                }
                if (string.Equals(index, TriggerMode))
                {
                    if (string.Equals(value, "软启动"))
                    {
                        triggerMode = 1;
                    }
                    if (string.Equals(value, "外触发"))
                    {
                        triggerMode = 0;
                    }
                }
                if (string.Equals(index, SampleFreq))
                {
                   
                   sampleFreqStr = value;
                   sampleFreq = Convert.ToDouble(value.ToString().ToUpper().Replace("HZ", "").Replace("K","000").Replace("M","000000"));
                }
                if (string.Equals(index, Range))
                {
                   range = Convert.ToDouble(value.ToString().ToUpper().Replace("V", ""));

                }
            }

        }
        public Hashtable cfgHashtable
        {
            get
            {
                return cfg;
            }
            set
            {
            }
        }
        bool gen = true;
        public void genData()
        {
            int pn = 0;
            pn = (int)(sampleFreq * 0.2);
            double t = 0.0;
            while(gen)
            {
                double [][]data = new double [12][];
                for (int i = 0; i < 12; i++)
                {
                    data[i] = new double[pn];
                }
                for (int i = 0; i < 5; i++)
                {
                    for (int j = 0; j < data[i].Length; j++)
                    {
                        data[i][j] = funU(t + 0.2/data[i].Length*j);
                    }
                }
                for (int i = 5; i < 12; i++)
                {
                    for (int j = 0; j < data[i].Length; j++)
                    {
                        data[i][j] = funI(t + 0.2 / data[i].Length * j);
                    }
                }
                dataExchange.Add(new DataValue(data, this));
                t += 0.2;//时间增加 200ms
                Thread.Sleep(199);
            }
        }
        public void stopGenData()
        {
            gen = false;
        }
        private double funU(double t)
        {
             double a1=  Math.PI / 3,a2 =  Math.PI /3;
             return 220 * Math.Sqrt(2) * Math.Cos(2 * Math.PI * 50 * (t + a1));// +20 * Math.Sqrt(2) * Math.Sin(2 * Math.PI * 50 * 2 * (t + a2));
        }

        private double funI(double t)
        {
            double a1 = Math.PI / 6, a2 = Math.PI / 3;
            a1 = 0;
            return 10 * Math.Sqrt(2) * Math.Cos(2 * Math.PI * 50 * (t + a1));// +20 * Math.Sqrt(2) * Math.Sin(2 * Math.PI * 50 * 2 * (t + a2));
        }

    }
}
